###########################################################################
#
#   Copyright 2010 American Public Media Group
#
#   This file is part of AIR2.
#
#   AIR2 is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   AIR2 is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

package AIR2::Searcher::Sources;
use strict;
use warnings;
use base qw( AIR2::Searcher );
use Carp;
use Data::Dump qw( dump );
use AIR2::Config;

my $source_config = {
    index         => AIR2::Config->get_search_index_path('sources'),
    default_field => 'source',
    facet_names   => [
        qw(
            smadd_state
            smadd_zip
            smadd_cntry
            political_affiliation
            src_household_income
            user_gender
            src_education_level
            user_ethnicity
            user_religion
            birth_year
            lifecycle
            timezone
            last_contacted_date
            last_response_date
            last_activity_date
            first_response_date
            org_uuid
            org_status
            src_status
            tag
            inq_uuid_title
            prj_uuid_title
            )
    ],
    fields => {
        1 => {
            alias_for => [
                qw( src_id
                    cem_email
                    src_last_name
                    src_first_name
                    src_username
                    src_uuid
                    birth_year
                    ethnicity
                    gender
                    employer
                    job_title
                    political_office
                    )
            ]
        },
        2 => { alias_for => [qw( qa sr_orig_value )] },

    },
};

# alias for an alias
$source_config->{fields}->{'bio'}     = $source_config->{fields}->{1};
$source_config->{fields}->{'profile'} = $source_config->{fields}->{1};

sub config { return $source_config }

my %no_snippets = map { $_ => $_ } qw(
    employer
    first_responded_date
    job_title
    last_activity_date
    last_contacted_date
    last_response_date
    org_name
    org_status
    org_status_date
    org_uuid
    primary_city
    primary_country
    primary_email
    primary_phone
    primary_state
    primary_zip
    qa
    so_org_id
    src_first_name
    src_last_name
    src_username
    src_uuid
    tag
    XXXask_code
    XXXcact_actm_id
    XXXcem_sub_date
    XXXincoming_activity_count
    XXXoutgoing_activity_count

);

my @no_hilite = ( keys %no_snippets, );

sub no_hilite    { return \@no_hilite }
sub engine_class {'+AIR2::Searcher::Sources::Engine'}

my %no_response_fields = map { $_ => $_ } qw(
    src_id

);

sub get_result_field_names {
    my $self        = shift;
    my $base_fields = $self->SUPER::get_result_field_names();

    # mask some internal-use-only fields from public API response
    my @fields = sort grep { !exists $no_response_fields{$_} } @$base_fields;

    return \@fields;
}

sub authz_field {'source.authz'}

sub snipper_config {
    return {
        occur         => 2,      # number of snips
        context       => 100,    # number of words in each snip
        as_sentences  => 1,
        ignore_length => 1,      # ignore max_chars, return entire snippet.
        show          => 0,      # only show if match, no dumb substr
        treat_phrases_as_singles => 0,    # keep phrases together
    };
}

1;
